import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

import middlewareConfig from './middleware.config';

// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1707907999471_5086';


  // egg框架安全性配置
  config.security = {
    // 关闭csrf
    csrf: {
      enable: false
    },
    domainWhiteList: ['*']
  };
  // 解决跨域、允许跨域方法
  config.cors = {
    origin: '*', // 匹配规则  域名+端口  *则为全匹配
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
    allowHeaders: 'version, User-Agent, Keep-Alive, Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, web-token, mp-token',
    credentials: true
  };

  // add your egg config in here
  config.middleware = [];

  // 自定义数据库默认表前缀
  config.mainDatabaseSchema = process.env.APP_MYSQL_PRE || 'pre_';
  config.databaseSchema = process.env.APP_MYSQL_PRE || 'pre_';

  // 自定义local端口
  config.cluster = {
    listen: {
      port: Number(process.env.APP_PORT) || 7003
    }
  };

  config.validate = {
    convert: true, // 对参数可以使用convertType规则进行类型转换
    // validateRoot: false, // 限制被验证值必须是一个对象。
  };

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
    ...middlewareConfig(),
  };
};
